package com.baidu.health.controller;


import com.baidu.health.pojo.CheckChunkResult;
import com.baidu.health.service.MediaUploadService;
import com.baidu.health.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
/**
 * Created by IntelliJ IDEA.
 * User: 孙新超
 *
 * @author 孙新超
 * @version 1.0
 * @date To change this template use File | Settings | File Templates.
 * @date
 */
@Slf4j
@RestController
@RequestMapping("/mediaUpload/upload")
@CrossOrigin
@Api(tags = "断点续传")
public class MediaUploadController {

    @Autowired
    MediaUploadService mediaUploadService;

    //    @GetMapping("test")
//    public  R test(){
//        return R.ok();
//    }
    //before-send-file
    @ApiOperation("上传")
    @PostMapping("/register")
    public R register(@RequestParam("fileMd5") String fileMd5,
                      @RequestParam("fileName") String fileName,
                      @RequestParam("fileSize") Long fileSize,
                      @RequestParam("mimetype") String mimetype,
                      @RequestParam("fileExt") String fileExt) {
        return mediaUploadService.register(fileMd5, fileName, fileSize, mimetype, fileExt);
    }
    @ApiOperation("分块")
    @PostMapping("/checkchunk")
    public CheckChunkResult checkchunk(@RequestParam("fileMd5") String fileMd5,
                                       @RequestParam("chunk") Integer chunk,
                                       @RequestParam("chunkSize") Integer chunkSize) {
        return mediaUploadService.checkchunk(fileMd5, chunk, chunkSize);
    }
    @ApiOperation("文件目录")
    @PostMapping("/uploadchunk")
    public R uploadchunk(@RequestParam("file") MultipartFile file,
                         @RequestParam("fileMd5") String fileMd5,
                         @RequestParam("chunk") Integer chunk) throws IOException {
        return mediaUploadService.uploadchunk(file, fileMd5, chunk);
    }

//    @PostMapping("/mergechunks")
//    @RequiresPermissions("file:video:list")
//    public R mergechunks(@RequestParam("fileMd5") String fileMd5,
//                         @RequestParam("fileName") String fileName,
//                         @RequestParam("fileSize") Long fileSize,
//                         @RequestParam("mimetype") String mimetype,
//                         @RequestParam("fileExt") String fileExt) {
//        SysUserEntity principal = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
//        Long userId = principal.getUserId();
//        return mediaUploadService.mergechunks(fileMd5, fileName, fileSize, mimetype, fileExt, userId);
//    }
}

